package com.example.qt.ptplatform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.qt.ptplatform.entity.CommentFavorite;
import com.example.qt.ptplatform.entity.vo.CommentFavoriteVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CommentFavoriteMapper extends BaseMapper<CommentFavorite> {

    @Select("SELECT COUNT(*) FROM comment_favorites WHERE user_id = #{userId} AND comment_id = #{commentId}")
    int existsByUserAndComment(@Param("userId") Integer userId,
                               @Param("commentId") Integer commentId);

    @Select("SELECT COUNT(*) FROM comment_favorites WHERE user_id = #{userId}")
    int countByUserId(@Param("userId") Integer userId);

    @Select("SELECT " +
            "c.comment_id, " +
            "c.torrent_id, " +
            "t.title AS torrent_name, " +  // 修改为torrent表中的title字段
            "c.comment_time, " +
            "u.username AS comment_username, " +
            "c.content, " +
            "c.likes_count, " +
            "c.reply_count " +
            "FROM comment_favorites cf " +
            "JOIN comments c ON cf.comment_id = c.comment_id " +
            "JOIN torrents t ON c.torrent_id = t.torrent_id " +  // 修改为torrent表
            "JOIN user u ON c.user_id = u.user_id " +  // 修改为user表
            "WHERE cf.user_id = #{userId} " +
            "ORDER BY cf.create_time DESC")
    List<CommentFavoriteVO> selectFavoriteCommentsByUserId(@Param("userId") Integer userId);
}